require 'rexml/document'
include REXML

class IndexRequest
  
  attr_accessor :url, :entity_id, :entity_name,:collection_id, :pick_single_roi, :gender

  def initialize(url, entity_id, entity_name, collection_id, pick_single_roi = nil, gender = nil)
      
      @url = url
      @entity_id = entity_id
      @entity_name = entity_name
      @collection_id = collection_id
      @pick_single_roi = pick_single_roi
      @gender = gender

  end

  def toXml()
    
    doc = Document.new()
    doc.add_element("index-request")
    node = Element.new("url")
    node.text = @url
    doc.root.elements << node
    
    node = Element.new("entity-id")
    node.text = @entity_id
    doc.root.elements << node
    
    node = Element.new("entity-name")
    node.text = @entity_id
    doc.root.elements << node
 
    if (@pick_single_roi != nil)
      node = Element.new("real-time")
      node.text = @pick_single_roi.to_s()
      doc.root.elements << node
    end    
        
    if (@gender != nil)
      node = Element.new("gender")
      node.text = @gender
      doc.root.elements << node
    end
    
    @collection_id.each do |id|
      node = Element.new("collection-id")
      node.text = id
      doc.root.elements << node
    end

    return doc.to_s()
    
  end
  
  def self.element_value(node, expression)
    
    elements = node.elements[expression]
    
    if (elements != nil) 
      return elements[0].to_s()
    else
      return nil
    end
    
  end

end